package com.mall.book;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.extension.incrementer.H2KeyGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.mall.book.common.AlipayHelper;
import com.mall.book.utils.JwtUtil;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SpringBootApplication
@MapperScan("com.mall.book.mappers")
public class BookApplication {

    public static void main(String[] args) {
        SpringApplication.run(BookApplication.class, args);
    }

    /* 拦截器 */
    @Configuration
    class MyWebMvcConfigurer implements WebMvcConfigurer {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry
                    .addInterceptor(new HandlerInterceptorAdapter() {
                        @Override
                        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                            String token = request.getHeader("Authorization");
                            if (token == null || "".equals(token)) {
                                return false;
                            }
                            System.out.println("token is : " + token);
                            //验证token过程如果没报错说明token有效,前端取数据需判断状态是否为401
                            JwtUtil.verifyToken(token);

                            return true;
                        }
                    })
                    .addPathPatterns("/sku/**")
                    .addPathPatterns("/manage/**")
                    .excludePathPatterns("");
            
        }
    }

    /* 加密器 */
    @Bean
    public BCryptPasswordEncoder encoder() {
        return new BCryptPasswordEncoder();
    }

    /* 主键策略 */
    @Bean
    public IKeyGenerator keyGenerator() {
        return new H2KeyGenerator();
    }

    /* alipay */
    @Bean
    public AlipayClient alipayClient() {
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayHelper.SERVER_URL,
                AlipayHelper.APP_ID,
                AlipayHelper.PRIVATE_KEY,
                AlipayHelper.FORMAT,
                AlipayHelper.CHARSET,
                AlipayHelper.ALIPAY_PUBLIC_KEY,
                AlipayHelper.SIGN_TYPE);
        return alipayClient;
    }

    /* mybatis-plus 插件集 */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());//乐观锁
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//分页拦截器
        return interceptor;
    }
}

